﻿package com.tvein.sanguo.swc.action.tool
{
	//flash
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import fl.controls.TextArea;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class ToolWorker extends MovieClip
	{
		public static const NAME:String = "Tool-Worker";

		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var workerArea:TextArea;
		
		public function ToolWorker()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			workerArea = new TextArea();
			workerArea.x = 30;
			workerArea.y = 30;
			workerArea.width = 340;
			workerArea.height = 210;
			this.addChild(workerArea);
			
			stage.focus = workerArea;
			
			btnBack.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(400, 300);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					var style:TextFormat = new TextFormat();
					style.color = 0xffffff;
					style.size = 16;
					style.font = "隶书";

					workerArea.setStyle("textFormat", style);
					workerArea.setStyle("upSkin",Sprite); 
			
					workerArea.text = params.Process[0].Value;
					break;
			}
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnBack":
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolWorker"]));
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnBack.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}